<!DOCTYPE html>
<html lang="">
<head>
    <title>检索</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="./style.css" rel="stylesheet" type="text/css">
    <link href="./font-awesome.min.css" rel="stylesheet">
    <link href="./foundation.min.css" rel="stylesheet" type="text/css">
    <link href="./foundation-datepicker.css" rel="stylesheet" type="text/css">
    <style>
        /* 添加响应式样式 */
        @media screen and (max-width: 1200px) {
            .main-box .chose {
                width: auto;
                padding: 0 10px;
            }

            .main-box .chose .box {
                flex-direction: column;
                align-items: flex-start;
            }

            .main-box .chose .box span {
                width: 100%;
                margin-bottom: 10px;
            }

            .main-box .chose .box ul {
                width: 100%;
                justify-content: flex-start;
            }

            .main-box .chose .box ul li {
                width: 48%; /* 两列布局 */
                margin-bottom: 10px;
            }

            .main-box .chose .box ul li:nth-of-type(even) {
                border-left: 1px solid #afafaf;
            }

            .main-box .chose .box .keyword-box input {
                width: calc(100% - 24px);
            }

            .main-box .table {
                width: auto;
                padding: 0 10px;
            }
        }

        @media screen and (max-width: 768px) {
            .main-box .chose .box ul li {
                width: 100%; /* 全宽布局 */
            }

            .main-box .chose .box ul li:nth-of-type(even) {
                border-left: none;
            }

            .main-box .chose .box .buttons button,
            .main-box .chose .box .keyword-box input {
                width: 100%;
            }

            .main-box .chose .box .buttons button {
                margin-top: 10px;
                width: auto;
            }
        }

        /* 加载中的动效样式 */
        .loading {
            display: none; /*默认情况下隐藏加载动效 */
            text-align: center;
            margin-top: 20px;
            font-size: 20px;
        }

        .loading::after {
            content: '.';
            animation: dots 1s steps(5, end) infinite;
        }

        @keyframes dots {
            0%, 20% {
                color: rgba(0, 0, 0, 0);
                text-shadow: .25em 0 0 rgba(0, 0, 0, 0), .5em 0 0 rgba(0, 0, 0, 0);
            }
            40% {
                color: #000;
                text-shadow: .25em 0 0 rgba(0, 0, 0, 0), .5em 0 0 rgba(0, 0, 0, 0);
            }
            60% {
                text-shadow: .25em 0 0 #000, .5em 0 0 rgba(0, 0, 0, 0);
            }
            100% {
                text-shadow: .25em 0 0 #000, .5em 0 0 #000;
            }
        }

    </style>
</head>
<body>
<div class="main-box">
    <div class="chose">
        <div class="box">
            <span>检索范围</span>
            <!--baidu：百度网盘，aliyundrive：阿里网盘，quark：夸克网盘，xunleipan：迅雷网盘-->
            <ul class="ul" id="resType">
                <li value="" class="act">默认</li>
                <li value="baidu">百度网盘</li>
                <li value="aliyundrive">阿里网盘</li>
                <li value="quark">夸克网盘</li>
                <li value="xunleipan">迅雷网盘</li>
            </ul>
        </div>
        <div class="box">
            <span>文件时间</span>
            <ul class="ul" id="diffDay">
                <li value="" class="act">全部</li>
                <li value="7">最近一周</li>
                <li value="30">最近一月</li>
                <li value="180">最近半年</li>
                <li value="365">最近一年</li>
            </ul>
        </div>

        <div class="box">
            <span>文件大小</span>
            <ul class="ul" id="fileSize">
                <li value="" class="act">全部</li>
                <li value="1-20971520">小于20M</li>
                <li value="20971520-209715200">20M-200M</li>
                <li value="209715200-2147483648">200M-2G</li>
                <li value="2147483648-1099511627776">大于2G</li>
            </ul>
        </div>
        <div class="box">
            <span>文件类型</span>
            <!--category：0:文件夹,1:视频,2:音乐.3:图片,5:软件,6:压缩包,7:BT种子，全部：不传-->
            <ul class="ul" id="category">
                <li value="" class="act">全部</li>
                <li value="0">文件夹</li>
                <li value="1">视频</li>
                <li value="2">音乐</li>
                <li value="3">图片</li>
                <li value="5">软件</li>
                <li value="6">压缩包</li>
                <li value="7">BT种子</li>
            </ul>
        </div>

        <div class="box">
            <span>检索模式</span>
            <ul class="ul" id="searchType">
                <li value="" class="act">默认</li>
                <li value="precise">精确</li>
                <li value="match">模糊</li>
            </ul>
        </div>

        <div class="box">
            <span>检索内容</span>
            <div class="keyword-box">
                <input name="keyword" id="keyword" placeholder="关键字">
            </div>
        </div>

        <div class="box">
            <div class="buttons">
                <button id="doSearch">查询</button>
                <button id="clear" class="clear">清除</button>
            </div>
        </div>
    </div>
    <div class="table">
        <table border="0" cellspacing="0" id="test">
            <thead>
            <tr>
                <th>文件名</th>
                <th>文件大小</th>
                <th>时间</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody id="data-box">
            <!-- 加载中的动效 -->
            <div class="loading" id="loading">加载中...</div>
            <!--            <tr>-->
            <!--                <td class="mf"> 00000</td>-->
            <!--                <td>11111</td>-->
            <!--                <td>22222</td>-->
            <!--                <td><a href="" class="link">获取链接</a></td>-->
            <!--            </tr>-->
            </tbody>
        </table>
    </div>
    <div id="page-box">
        <ul id="pagination" class="pagination"></ul>
    </div>
</div>
<script src="./jquery.min.js"></script>
<script src="./foundation-datepicker.min.js"></script>
<script src="./foundation-datepicker.zh-CN.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $('#doSearch').on('click', function () {
            loadData(1);
        });

        function loadData(page) {
            const keyword = $('#keyword').val();
            if (keyword.trim() === '') {
                alert("请输入关键字");
                return;
            }

            $('#data-box').html('');
            $('#pagination').html('');
            $("#loading").show();

            const diffDay = $('#diffDay li.act').attr("value");
            const resType = $('#resType li.act').attr("value");
            const category = $('#category li.act').attr("value");
            const fileSize = $('#fileSize li.act').attr("value");

            let minSize = 0;
            let maxSize = 0;
            if (fileSize !== '') {
                let fileSizeArr = fileSize.split('-');
                minSize = fileSizeArr[0];
                maxSize = fileSizeArr[1];
            }
            const searchType = $('#searchType li.act').attr("value");

            let params = "t=" + Date.now() + "&version=v2" + "&line=0&site=dalipan";

            params += "&kw=" + keyword;
            params += category === '' ? '' : "&category=" + category;
            params += diffDay === '' ? '' : "&diffDay=" + diffDay;
            params += searchType === '' ? '' : "&searchType=" + searchType;
            params += resType === '' ? '' : "&resType=" + resType;
            params += minSize === 0 ? '' : "&minSize=" + minSize;
            params += maxSize === 0 ? '' : "&maxSize=" + maxSize;
            params += "&page=" + page;

            $.ajax({
                url: 'https://fc-resource-node-api.krzb.net/api/v1/pan/search?' + params, // 替换为你的后端搜索API路径
                method: 'GET',
                data: {},
                success: function (data) {
                    $("#loading").hide();
                    displayResults(page, data);
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    $("#loading").hide();
                    console.error('Error fetching search results:', textStatus, errorThrown);
                    alert('搜索时发生错误，请重试。');
                }
            });
        }

        function displayResults(page, result) {
            const dataBox = $('#data-box');
            const pagination = $('#pagination');
            dataBox.html('');
            pagination.html('');
            const rows = result.resources;
            const total = result.total;
            if (rows && rows.length > 0) {
                rows.forEach(function (info) {
                    const item = info.res;
                    const id = item.id === 'login_first' ? '' : item.id;
                    const itemInfo = "<tr>" +
                        "<td class=\"mf\">" + item.filename + "</td>" +
                        "<td>" + bytesToSize(item.size) + "</td>" +
                        "<td>" + item.updatetime + "</td>" +
                        "<td><a id=\"clink\" cid=\"" + id + "\" pid=\"" + item.parent + "\" ctype=\"" + item.type + "\" class=\"link\">获取链接</a></td>" +
                        "</tr>"
                    dataBox.append(itemInfo);
                });
                //写入分页
                if (total > 0) {
                    //计算页码
                    const pageCountReal = Math.min(Math.ceil(total / 15), 666);

                    function createPagination(totalPages, currentPage) {
                        const $pagination = $('#pagination');

                        // 清空现有的分页内容
                        $pagination.empty();

                        const addPageButton = (page, text = page) => {
                            const $li = $('<li></li>');
                            const $a = $('<a class="pageClick"></a>').text(text).attr('data-page', `${page}`);
                            if (page === currentPage) {
                                $a.addClass('active');
                            }
                            $li.append($a);
                            $pagination.append($li);
                        };

                        const addEllipsis = () => {
                            const $li = $('<li></li>').addClass('disabled');
                            const $a = $('<a class="pageClick"></a>').text('...');
                            $li.append($a);
                            $pagination.append($li);
                        };

                        if (totalPages <= 10) {
                            for (let i = 1; i <= totalPages; i++) {
                                addPageButton(i);
                            }
                        } else {
                            if (currentPage <= 6) {
                                // 显示前7页
                                for (let i = 1; i <= 7; i++) {
                                    addPageButton(i);
                                }
                                addEllipsis();
                                // 显示最后2页
                                for (let i = totalPages - 1; i <= totalPages; i++) {
                                    addPageButton(i);
                                }
                            } else {
                                // 显示前2页
                                for (let i = 1; i <= 2; i++) {
                                    addPageButton(i);
                                }

                                addEllipsis();

                                // 显示当前页及当前页的左右各2页
                                const startPage = Math.max(3, currentPage - 2);
                                const endPage = Math.min(currentPage + 2, totalPages - 2);

                                for (let i = startPage; i <= endPage; i++) {
                                    addPageButton(i);
                                }

                                if (endPage < totalPages - 2) {
                                    addEllipsis();
                                }

                                // 显示最后2页
                                for (let i = totalPages - 1; i <= totalPages; i++) {
                                    addPageButton(i);
                                }
                            }
                        }
                    }

                    createPagination(pageCountReal, page);
                }
            } else {
                dataBox.text('没有找到匹配的结果。');
            }
        }

        $(document).on('click', '.pageClick', function () {
            const page = $(this).attr('data-page');
            loadData(parseInt(page));
        });

        $(document).on('click', '#clink', function () {
            const id = $(this).attr('cid');
            if ("" === id) {
                alert("请先登录");
                return;
            }
            const type = $(this).attr('ctype');
            const parent = $(this).attr('parent');
            const params = "t=" + Date.now() + "&version=v2" + "&id=" + id + "&size=15&type=" + type + "&parent=" + parent;
            $.ajax({
                url: 'https://fc-resource-node-api.krzb.net/api/v1/pan/detail?' + params, // 替换为你的后端搜索API路径
                method: 'GET',
                data: {},
                success: function (data) {
                    const pwd = data.pwd;
                    const hasPwd = data.haspwd;
                    const filename = data.filename;
                    const params = "t=" + Date.now() + "&version=v2" + "&id=" + id;
                    $.ajax({
                        url: 'https://fc-resource-node-api.krzb.net/api/v1/pan/url?' + params, // 替换为你的后端搜索API路径
                        method: 'GET',
                        data: {},
                        success: function (data) {
                            let fullLink = data.data;
                            if (hasPwd) {
                                fullLink = data.data + "?pwd=" + pwd;
                            }
                            const exInfo = id + "|" + parent + "|" + data.data + "|" + pwd + "|" + type + "|" + filename;
                            window.java.showMessage(fullLink, exInfo);
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            console.error('Error fetching search results:', textStatus, errorThrown);
                            alert('搜索时发生错误，请重试。');
                        }
                    });
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    console.error('Error fetching search results:', textStatus, errorThrown);
                    alert('搜索时发生错误，请重试。');
                }
            });
        });

        function bytesToSize(bytes) {
            if (bytes === 0) return '0 B';

            const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
            const i = Math.floor(Math.log(bytes) / Math.log(1024));

            return parseFloat((bytes / Math.pow(1024, i)).toFixed(2)) + ' ' + sizes[i];
        }

        $(".buttons .clear").click(function () {
            act($('#diffDay li:first'));
            act($('#resType li:first'));
            act($('#category li:first'));
            act($('#fileSize li:first'));
            act($('#searchType li:first'));
            $('#keyword').val('');
        });

        $(".ul li").click(function () {
            act($(this));
        });

        function act(obj) {
            obj.siblings('li').removeClass('act');
            obj.siblings('li').next('li').removeClass('act2');
            obj.addClass('act');
            obj.next('li').addClass('act2');
        }
    });
</script>
</body>
</html>